﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.ComponentModel;
using BaseTypesControlLib.Model;

namespace BaseTypesControlLib.Controls
{
    public enum ListItemType
    {
        ImageSubtitle, ImageTitle, Subtitle, RightImageSubtitle, RightImageTitle
    }
    public class CustomListFieldControl:FieldControl, INotifyPropertyChanged
    {
        public CustomListFieldControl()
        {
            DefaultStyleKey = typeof(CustomListFieldControl);
        }
        static CustomListFieldControl()
        {
            FrameworkPropertyMetadata fpm = new FrameworkPropertyMetadata(ListItemType.ImageSubtitle,
                FrameworkPropertyMetadataOptions.AffectsMeasure |
                FrameworkPropertyMetadataOptions.AffectsArrange, OnTypeChanged);

            ListItemTypeProperty = DependencyProperty.Register("ListItemType", typeof(ListItemType), 
                typeof(CustomListFieldControl), fpm);
        }
        protected override Size MeasureOverride(Size constraint)
        {
            Size sz = base.MeasureOverride(constraint);
            sz.Width = constraint.Width;
            return sz;
        }

        public ListItemType ListItemType
        {
            get { return (ListItemType)GetValue(ListItemTypeProperty); }
            set { SetValue(ListItemTypeProperty, value); }
        }
        public static readonly DependencyProperty ListItemTypeProperty;

        private static void OnTypeChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            CustomListFieldControl ctrl = o as CustomListFieldControl;
            if (ctrl != null)
                ctrl.OnPropertyChanged("Items");
        }

        public List<ListItem> Items
        {
            get
            {
                List<ListItem> res = new List<ListItem>();
                for (int i = 0; i < 10; i++)
                {
                    ListItem li = null;
                    switch (ListItemType)
                    {
                        case ListItemType.ImageSubtitle:
                            li = new ImageSubtitleListItem();
                            break;
                        case ListItemType.ImageTitle:
                            li = new ImageTitleListItem();
                            break;
                        case ListItemType.RightImageSubtitle:
                            li = new RightImageSubtitleListItem();
                            break;
                        case ListItemType.RightImageTitle:
                            li = new RightImageTitleListItem();
                            break;
                        default:
                            li = new SubtitleListItem();
                            break;
                    }
                    res.Add(li);
                }
                return res;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
}
